<?php

namespace Admin\Controller;

use Think\Controller;

class AccountController extends Controller {

    /**
     * 显示帐号信息
     */
    public function index(){
        $user = obtain();
        $result = M('user')->where("userAccount='%s'",$user)->find();
        $this->assign('user', $result);
        $this->display('index');
        
    }
    //维护
    public function maintain(){
        $user = obtain();
        if(empty($_POST)){
            $result = M('user')->where("userAccount='%s'",$user)->find();
            $this->assign('user', $result);
            $this->display('maintain');
        }else{
            $model = M('user');
            $data = $model->create();
            if( $data && $model->where("userAccount='%s'",$user)->save($data)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }
    
    //修改密码
    public function password(){
        $user = obtain();
        if(empty($_POST)){
            $this->display('password');
        }else{
            $jpwd = I('post.jpwd');
            $jpwd = encrypt($jpwd);
            if(!M('user')->where("userAccount='%s' and password='%s'",$user,$jpwd)->count()){
               $this->error('密码错误'); 
            }
            $pwd = I('post.pwd');
            $pwd = encrypt($pwd);
            $data = array('password'=>$pwd);
            if(M('user')->where("userAccount='%s' and password='%s'",$user,$jpwd)->save($data)){
               $this->error('修改成功'); 
            }else{
                $this->error('修改失败');
            }
        }
    }
}